//-----------------------------------------------------------------------
// <copyright file="CreateCertificateMapping.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <summary>Add a user/certificate mapping</summary>
//-----------------------------------------------------------------------
namespace Microsoft.Sdc.Tasks.Web.WebSite
{
    using System;
    using System.Security.Cryptography.X509Certificates;
    using Microsoft.Build.Framework;

    /// <summary>
    /// Add a user/certificate mapping
    /// </summary>
    public class CreateCertificateMapping : TaskBase
    {
        /// <summary>
        /// The description of the web site to locate
        /// </summary>
        private string description;

        /// <summary>
        /// The identifier of thw website to locate
        /// </summary>
        private string identifier;

        /// <summary>
        /// The path of the certificate to load to use for the mapping
        /// </summary>
        private string certificatePath;

        /// <summary>
        /// The password of the certificate
        /// </summary>
        private string certificatePassword;

        /// <summary>
        /// The username to map to
        /// </summary>
        private string accountUsername;

        /// <summary>
        /// The password of the user that is being mapped
        /// </summary>
        private string accountPassword;

        /// <summary>
        /// A friendly name
        /// </summary>
        private string accountFriendlyName;
        private string machineName = "localhost";

        /// <summary>
        /// Machine to operate on
        /// </summary>
        public string MachineName
        {
            get { return this.machineName; }
            set { this.machineName = value; }
        }

        /// <summary>
        /// A path to a certificate that is to be mapped
        /// </summary>
        [Required]
        public string CertificatePath
        {
            get { return this.certificatePath; }

            set { this.certificatePath = value; }
        }

        /// <summary>
        /// The password of the certificate
        /// </summary>
        [Required]
        public string CertificatePassword
        {
            get { return this.certificatePassword; }

            set { this.certificatePassword = value; }
        }

        /// <summary>
        /// The username that is to be mapped
        /// </summary>
        [Required]
        public string AccountUsername
        {
            get { return this.accountUsername; }

            set { this.accountUsername = value; }
        }

        /// <summary>
        /// The password of the user being mapped
        /// </summary>
        [Required]
        public string AccountPassword
        {
            get { return this.accountPassword; }

            set { this.accountPassword = value; }
        }

        /// <summary>
        /// A friendly name to be displayed in the mapper
        /// </summary>
        [Required]
        public string AccountFriendlyName
        {
            get { return this.accountFriendlyName; }

            set { this.accountFriendlyName = value; }
        }

        /// <summary>
        /// Description of web site to modify
        /// </summary>
        /// <value>A valid web site description</value>
        /// <remarks>If this parameter is not specified you must specify the site <seealso cref="Identifier">Identifier</seealso> 
        /// for the site you wish to start</remarks>
        public string Description
        {
            get { return (this.description ?? String.Empty); }

            set { this.description = value; }
        }

        /// <summary>
        /// web site idenfitier to modify
        /// </summary>
        /// <value>A valid web site identifier</value>
        /// <remarks>If this parameter is not specified you must specify the site <seealso cref="Description">Description</seealso> 
        /// for the site you wish to start</remarks>
        public string Identifier
        {
            get { return (this.identifier ?? String.Empty); }

            set { this.identifier = value; }
        }

        /// <summary>
        /// map a certificate to an NT user account
        /// </summary>
        protected override void InternalExecute()
        {
            if (this.description == null && this.identifier == null)
            {
                throw new TaskException("Web.DescriptionOrIdentifier");
            }

            if (this.description != null && this.identifier != null)
            {
                throw new TaskException("Web.DescriptionOrIdentifierNotBoth", this.description, this.identifier);
            }

            int id = Utilities.CheckWebSiteDescriptionOrIdentifier(this.description, this.identifier, "Create Certificate Mapping", this.MachineName);
            if (Microsoft.Sdc.Tasks.Configuration.Web.WebSite.Exists(this.MachineName, id))
            {
                Microsoft.Sdc.Tasks.Configuration.Web.WebSite x = Microsoft.Sdc.Tasks.Configuration.Web.WebSite.Load(this.MachineName, id);

                X509Certificate cert = new X509Certificate();
                cert.Import(this.CertificatePath, this.CertificatePassword, X509KeyStorageFlags.MachineKeySet);

                x.CreateCertificateMapping(cert.GetRawCertData(), this.AccountUsername, this.AccountPassword, this.AccountFriendlyName);
            }
        }
    }
}